import { Controller, Get, Post, Put, Body, Param, Delete } from '@nestjs/common'
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger'
import { CompanyClass, CompanyModel } from './company.model'

@ApiBearerAuth()
@Controller('company')
@ApiTags('公司管理')
export class CompanyController {
  @Get('query')
  // @ApiQuery({ name: 'page', description: '分页信息' })
  @ApiOperation({
    summary: '公司列表',
    description: '显示全部公司列表',
  })
  async findAll() {
    return await CompanyModel.find().select('-__v')
  }

  //分页不成功，存在循环依赖问题
  // @Get(':pageParam')
  // @ApiOperation({
  //   summary: '公司列表',
  //   description: '分页显示公司详情',
  // })
  // async findByPage(@Param() pageParam: Pagination) {
  //   const { currentPage, pageSize } = pageParam
  //   const skipNum = (currentPage - 1) * pageSize
  //   return await CompanyModel.find().skip(skipNum).limit(pageSize).exec()
  // }

  @Get(':id')
  @ApiOperation({
    summary: '获取公司',
    description: '根据id获取一条公司详情',
  })
  async detail(@Param('id') id: string) {
    return await CompanyModel.findById<CompanyClass>(id).select('-__v')
  }

  @Post('create')
  @ApiOperation({
    summary: '创建详情',
    description: '创建一条公司详情',
  })
  async create(@Body() createCompany: CompanyClass) {
    await CompanyModel.create(createCompany)
    return {
      message: '新公司创建成功！',
      success: true,
    }
  }

  @Put(':id')
  @ApiOperation({
    summary: '编辑详情',
    description: '根据id编辑一条公司详情',
  })
  async update(@Param('id') id: string, @Body() updateCompany: CompanyClass) {
    await CompanyModel.findByIdAndUpdate(id, updateCompany, {
      runValidators: true,
    }).exec()
    return {
      success: true,
      message: '公司更新成功！',
    }
  }
  @Delete(':id')
  @ApiOperation({
    summary: '删除',
    description: '根据id删除一条公司详情',
  })
  async remove(@Param('id') id: string) {
    await CompanyModel.findByIdAndDelete(id).exec()
    return {
      success: true,
      message: '公司删除成功！',
    }
  }
}
